
package com.csc440.team2.smil;

import java.io.IOException;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;


import org.xmlpull.v1.XmlSerializer;


import android.util.Log;
import android.util.Xml;


import com.csc440.team2.SmilConstants;


public class SMILMessage implements Serializable
{
    private static final long serialVersionUID = 1L;
    private List<SMILComponent> componentList;
    private String subject;


    public static SMILMessage create()
    {
        SMILMessage message = new SMILMessage();
        message.setComponentList(new ArrayList<SMILComponent>());
        return message;
    }


    private SMILMessage()
    {

    }


    /**
     * @return the seriesList
     */
    public List<SMILComponent> getComponentList()
    {
        return componentList;
    }


    /**
     * @param seriesList the seriesList to set
     */
    public void setComponentList(List<SMILComponent> componentList)
    {
        this.componentList = componentList;
    }


    public void addComponent(SMILComponent component)
    {
        componentList.add(component);
    }


    /**
     * @return the subject
     */
    public String getSubject()
    {
        return subject;
    }


    /**
     * @param subject the subject to set
     */
    public void setSubject(String subject)
    {
        this.subject = subject;
    }


    public String getXml()
    {
        StringBuffer xml = new StringBuffer();
        xml.append("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><smil><body><par>");

        for (SMILComponent component : componentList)
        {
            switch (component.getType())
            {
                case SmilConstants.COMPONENT_TYPE_TEXT:
                {
                    SMILComponent comp = component;
                    xml.append(getXml(comp));
                    break;
                }

            }
        }

        xml.append("</par></body></smil>");

        return xml.toString();

    }


    public String getXml(SMILComponent comp)
    {
        XmlSerializer serializer = Xml.newSerializer();
        try
        {
            StringWriter writer = new StringWriter();

            serializer.setOutput(writer);
            // set indentation option
            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

            switch (comp.getType())
            {
                case SmilConstants.COMPONENT_TYPE_TEXT:
                {
                    SMILTextComponent textComponent = (SMILTextComponent) comp;
                    serializer.startTag(null, "text");
                    serializer.attribute(null, "dur", "" + textComponent.getDuration());
                    serializer.text(textComponent.getText());
                    serializer.endTag(null, "text");
                    break;
                }
                case SmilConstants.COMPONENT_TYPE_IMAGE:
                {
                    SMILImageComponent imageComponent = (SMILImageComponent) comp;
                    serializer.startTag(null, "img");
                    serializer.attribute(null, "dur", "" + imageComponent.getDuration());
                    serializer.text(imageComponent.getText());
                    serializer.endTag(null, "img");
                    break;
                }
                case SmilConstants.COMPONENT_TYPE_AUDIO:
                {
                    SMILAudioComponent audioComponent = (SMILAudioComponent) comp;
                    serializer.startTag(null, "audio");
                    serializer.attribute(null, "dur", "" + audioComponent.getDuration());
                    serializer.text(audioComponent.getText());
                    serializer.endTag(null, "audio");
                    break;
                }
                case SmilConstants.COMPONENT_TYPE_VIDEO:
                {
                    SMILVideoComponent videoComponent = (SMILVideoComponent) comp;
                    serializer.startTag(null, "video");
                    serializer.attribute(null, "dur", "" + videoComponent.getDuration());
                    serializer.text(videoComponent.getText());
                    serializer.endTag(null, "video");
                    break;
                }
            }

            serializer.endDocument();
            serializer.flush();

            return writer.toString();
        }
        catch (IOException ioe)
        {
            Log.e("IOException", "Error serializing XML", ioe);
            return "";
        }
    }


    @Override
    public String toString()
    {
        return subject;
    }

}
